package com.itheima.Service.imple;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.StudentService;
import com.itheima.entity.EmpParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Student;
import com.itheima.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean getListStudent(EmpParam empParam) {
        //设置启动参数（使用pagehelper）
        PageHelper.startPage(empParam.getPage(), empParam.getPageSize());
        //调用mapper层获取数据
        List<Student> studentList = studentMapper.getListStudent(empParam);
        Page p =(Page)studentList;
        return new PageBean(p.getTotal(),p.getResult());
    }
    //批量删除学员（根据id）
    @Override
    public void deleteStudents(List<Integer> ids) {
        //调用mapper层
        studentMapper.deleteStudents(ids);
    }
    //添加学员
    @Override
    public void addstudent(Student student) {
        //补全数据
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationScore(Short.valueOf("0"));
        student.setViolationCount(Short.valueOf("0"));
        //调用mapper层
        studentMapper.addSyudent(student);
    }
    //根据id查询学员信息
    @Override
    public Student getStudentById(Integer id) {
        //调用mapper层
        return studentMapper.getStudentById(id);
    }
    //修改学员的数据信息
    @Override
    public void updateStudent(Student student) {
        //调用mapper层
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }
    //学员违纪处理操作
    @Override
    public void violation(Integer id, Integer score) {
        //调用mapper层
        //先查询学员相关数据再对数据进行操作
        Student student = studentMapper.getStudentById(id);
        Short Count = student.getViolationCount();
        //违纪次数+1
         Count++;
        //违纪扣分累加
        Short Score = student.getViolationScore();
        score = score + Score;
        studentMapper.violation(id,score,Count);
    }
}
